﻿using SmartPatientCentre.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartPatientCentre.Repositories // DO NOT USE!!!!! FOR TESTING ONLY
{
    class UniqueResultRepository
    {
        private List<UniqueResult> _allList = new List<UniqueResult>();
        public List<UniqueResult> AllList { get { return _allList; } }

        private List<UniqueResult> _sodiumList = new List<UniqueResult>();
        public List<UniqueResult> SodiumList { get { return _sodiumList; } }

        private List<UniqueResult> _potassiumList = new List<UniqueResult>();
        public List<UniqueResult> PotassiumList { get { return _potassiumList; } }

        private List<UniqueResult> _ureaList = new List<UniqueResult>();
        public List<UniqueResult> UreaList { get { return _ureaList; } }

        private List<UniqueResult> _creatinineList = new List<UniqueResult>();
        public List<UniqueResult> CreatinineList { get { return _creatinineList; } }


        // Create class and Populate List<UniqueResult> from List<Result>
        public UniqueResultRepository(List<Result> resultList)
        {
            foreach (Result result in resultList)
            {
                this.CreateUniqueResult(result);
            }

            this.OrderByDate();
            this.CalculateDifference(SodiumList);
            this.CalculateDifference(PotassiumList);
            this.CalculateDifference(UreaList);
            this.CalculateDifference(CreatinineList);
        }

        // Create UniqueResult instances from Result
        // Also add to List<UniqueResult>
        private void CreateUniqueResult(Result result)
        {
            UniqueResult sodiumResult = new SodiumResult(result);
            _allList.Add(sodiumResult);
            _sodiumList.Add(sodiumResult);

            UniqueResult potassiumResult = new PotassiumResult(result);
            _allList.Add(potassiumResult);
            _potassiumList.Add(potassiumResult);

            UniqueResult ureaResult = new UreaResult(result);
            _allList.Add(ureaResult);
            _ureaList.Add(ureaResult);

            //UniqueResult creatinineResult = new CreatinineResult(result);
            //_allList.Add(creatinineResult);
            //_creatinineList.Add(creatinineResult);
        }

        // Order Result lists by Date
        private void OrderByDate()
        {
            _allList = AllList.OrderByDescending(r => r.Date).ToList();
            _sodiumList = SodiumList.OrderByDescending(r => r.Date).ToList();
            _potassiumList = PotassiumList.OrderByDescending(r => r.Date).ToList();
            _ureaList = UreaList.OrderByDescending(r => r.Date).ToList();
            _creatinineList = CreatinineList.OrderByDescending(r => r.Date).ToList();
        }

        // Method to calculate result difference from previous
        private void CalculateDifference(List<UniqueResult> list)
        {
            int listCount = list.Count;

            foreach (var result in list)
            {
                int previousResult = list.IndexOf(result) + 1;

                if (previousResult < listCount) // Check if the result is not last in the list
                {
                    result.Difference = result.Value - list[previousResult].Value;
                }
                else // No previous result to compare
                {
                    result.Difference = 0;
                }
            }
        }
    }
}
